package org.zjvis.datascience.common.dto;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.zjvis.datascience.common.config.DatasourceConfig;
import org.zjvis.datascience.common.constant.DatabaseConstant;

import static org.zjvis.datascience.common.constant.DatabaseConstant.DEFAULT_DATASET_ID;
import static org.zjvis.datascience.common.constant.DatabaseConstant.DEFAULT_DATASET_NAME;

/**
 * @description 数据源信息表，数据驱动DTO
 * @date 2020-10-09
 */
@Data
public class DatabaseDTO extends BaseDTO {

    private Long id;

    private String name;

    private Integer type;

    private String configJson;

    private String description;

    public String getConnectionURL(int type) {
        String url = "";
        JSONObject jsonObj = JSONObject.parseObject(configJson);
        String address = jsonObj.getString("address");
        int port = jsonObj.getIntValue("port");
        String db = jsonObj.getOrDefault("db","").toString();

        if (DatabaseConstant.DATASOURCE_TYPE_GREENPLUM == type) {
            url = String.format("jdbc:pivotal:greenplum://%s:%s;DatabaseName=%s", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_MYSQL == type) {
            url = String.format("jdbc:mysql://%s:%s/%s?characterEncoding=UTF-8", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_HIVE == type) {
            url = String.format("jdbc:hive2://%s:%s/", address, port);
        } else if (DatabaseConstant.DATASOURCE_TYPE_ELASTICSEARCH == type) {
            url = String.format("jdbc:presto://%s:%s/elasticsearch/%s", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_TIDB == type) {
            url = String.format("jdbc:presto://%s:%s/tidb/%s", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_SQLSERVER == type) {
            url = String.format("jdbc:sqlserver://%s:%s", address, port);
            if (StringUtils.isNotEmpty(db)) { url = url + ";DatabaseName=" + db; }
        } else if (DatabaseConstant.DATASOURCE_TYPE_ORACLE == type) {
            url = String.format("jdbc:oracle:thin:@%s:%s/%s", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_HANA == type) {
            url = String.format("jdbc:sap://%s:%s/%s?reconnect=true", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_DRUID == type) {
            url = String.format("jdbc:avatica:remote:url=http://%s:%s/druid/v2/sql/avatica/", address, port);
        } else if (DatabaseConstant.DATASOURCE_TYPE_CLICKHOUSE == type) {
            url = String.format("jdbc:clickhouse://%s:%s/%s", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_POSTGRES == type) {
            url = String.format("jdbc:postgresql://%s:%s/%s?stringtype=unspecified", address, port, db);
        }
        return url;
    }

    public String getConnectionURL() {
        String url = "";
        JSONObject jsonObj = JSONObject.parseObject(configJson);
        String address = jsonObj.getString("address");
        int port = jsonObj.getIntValue("port");
        String db = jsonObj.getOrDefault("db","").toString();

        if (type == null) {
            return String.format("jdbc:pivotal:greenplum://%s:%s;DatabaseName=%s", address, port, db);
        }else if (DatabaseConstant.DATASOURCE_TYPE_GREENPLUM == type) {
            url = String.format("jdbc:pivotal:greenplum://%s:%s;DatabaseName=%s", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_MYSQL == type) {
            url = String.format("jdbc:mysql://%s:%s/%s?characterEncoding=UTF-8", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_HIVE == type) {
            url = String.format("jdbc:hive2://%s:%s/", address, port);
        } else if (DatabaseConstant.DATASOURCE_TYPE_ELASTICSEARCH == type) {
            url = String.format("jdbc:presto://%s:%s/elasticsearch/%s", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_TIDB == type) {
            url = String.format("jdbc:presto://%s:%s/tidb/%s", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_SQLSERVER == type) {
            url = String.format("jdbc:sqlserver://%s:%s", address, port);
            if (StringUtils.isNotEmpty(db)) { url = url + ";DatabaseName=" + db; }
        } else if (DatabaseConstant.DATASOURCE_TYPE_ORACLE == type) {
            url = String.format("jdbc:oracle:thin:@%s:%s/%s", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_HANA == type) {
            url = String.format("jdbc:sap://%s:%s/%s?reconnect=true", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_DRUID == type) {
            url = String.format("jdbc:avatica:remote:url=http://%s:%s/druid/v2/sql/avatica/", address, port);
        } else if (DatabaseConstant.DATASOURCE_TYPE_CLICKHOUSE == type) {
            url = String.format("jdbc:clickhouse://%s:%s/%s", address, port, db);
        } else if (DatabaseConstant.DATASOURCE_TYPE_POSTGRES == type) {
            url = String.format("jdbc:postgresql://%s:%s/%s?stringtype=unspecified", address, port, db);
        }

        return url;
    }

    public String getJDBCDriver(){
        JSONObject config = JSONObject.parseObject(this.getConfigJson());
        return config.getOrDefault("driver", DatasourceConfig.Driver.Postgres.getDriver()).toString();
    }

    public DatabaseDTO getDefaultDB(JSONObject configJson ){
        this.setId(DEFAULT_DATASET_ID);
        this.setName(DEFAULT_DATASET_NAME);
        if (configJson.getString("driver").equals("org.postgresql.Driver")){
            this.setType(10);
        }else if (configJson.getString("driver").equals("com.pivotal.jdbc.GreenplumDriver")){
            this.setType(0);
        }
        this.setConfigJson(configJson.toJSONString());
        return this;
    }


}
